package com.lzx.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzx.article.entity.Articles;
import com.lzx.article.entity.TwoSort;
import com.lzx.article.entity.UserRecommend;
import com.lzx.article.feign.UserClient;
import com.lzx.article.mapper.ArticlesMapper;
import com.lzx.article.mapper.TwoSortMapper;
import com.lzx.article.mapper.UserRecommendMapper;
import com.lzx.article.service.UserRecommendService;
import com.lzx.article.utils.TypeConversion;
import com.lzx.article.vo.User;
import com.lzx.common.utils.R;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Random;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lzx
 * @since 2023-02-05
 */
@Service
public class UserRecommendServiceImpl extends ServiceImpl<UserRecommendMapper, UserRecommend> implements UserRecommendService {
    @Resource
    private UserRecommendMapper userRecommendMapper;

    @Resource
    private ArticlesMapper articlesMapper;

    @Resource
    private TwoSortMapper twoSortMapper;

    @Resource
    private UserClient userClient;

    //用户文章推荐
    @Override
    public R userArticleRecommend(Long userId) {
        QueryWrapper<UserRecommend> userRecommendQueryWrapper = new QueryWrapper<>();
        userRecommendQueryWrapper.eq("user_id", userId);
        UserRecommend userRecommend = userRecommendMapper.selectOne(userRecommendQueryWrapper);
        //文章总数(包括逻辑删除的)
        Long count = articlesMapper.selectArticleCount();
        if (count < 3) {
            return R.error().message("文章总数少于3，无法推荐!");
        }
        //推荐文章列表
        List<Articles> articleList = new ArrayList<>();
        if (userRecommend == null || userId == null) {
            //用户推荐表中不存在当前用户的推荐文章或用户未登录，则随机推荐文章给用户
            Random random = new Random();
            List<Integer> articleIds = new ArrayList<>();
            HashSet<Integer> set = new HashSet<>();
            do {
                int articleId = random.nextInt(Math.toIntExact(count)) + 1;
                Articles articles = articlesMapper.selectArticleIsDelete((long) articleId);
                if (articles.getStatus() == 0) {
                    if (set.add(articleId)) {
                        set.add(articleId);
                        articleIds.add(articleId);
                    }
                }
            } while (articleIds.size() < 3);
            for (Integer articleId : articleIds) {
                Articles articles = articlesMapper.selectById(articleId);
                articleList.add(articles);
            }
        } else {
            List<String> articleIds = TypeConversion.stringToList(userRecommend.getArticleIds());
            articleIds.forEach(articleId -> articleList.add(articlesMapper.selectById(articleId)));
        }
        //设置用户信息
        for (Articles articles : articleList) {
            User user = userClient.getUserinfoByIdToOtherServe(Long.valueOf(articles.getUserId()));
            articles.setUser(user);
        }
        return R.ok().data("list", articleList);
    }

    //文章详情页面中文章推荐
    @Override
    public R articleDetailsRecommend(Long articleId) {
        Articles articles = articlesMapper.selectById(articleId);
        List<String> labelIds = TypeConversion.stringToList(articles.getLabel());
        //防止文章重复
        HashSet<Long> set = new HashSet<>();
        //推荐十篇文章
        ArrayList<Articles> list = new ArrayList<>();
        for (String labelId : labelIds) {
            TwoSort twoSort = twoSortMapper.selectById(labelId);
            QueryWrapper<Articles> articlesQueryWrapper = new QueryWrapper<>();
            articlesQueryWrapper.like("label_translation", twoSort.getTagName());
            List<Articles> articlesList = articlesMapper.selectList(articlesQueryWrapper);
            for (Articles vo : articlesList) {
                if (set.add(vo.getId())) {
                    set.add(vo.getId());
                    //设置用户信息
                    User user = userClient.getUserinfoByIdToOtherServe(Long.valueOf(vo.getUserId()));
                    vo.setUser(user);
                    list.add(vo);
                    if (list.size() >= 10) {
                        return R.ok().data("list", list);
                    }
                }
            }
        }
        return R.ok().data("list", list);
    }
}
